package com.hlxy.fixdb.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.hlxy.fixdb.bean.COrgExtra;
import com.hlxy.fixdb.dao.pre.PreCOrgExtraDao;
import com.hlxy.fixdb.dao.pro.ProCOrgExtraDao;
import com.hlxy.fixdb.service.COrgExtraService;

@Component("cOrgExtraService")
public class COrgExtraServiceImpl implements COrgExtraService{
	
	@Autowired
	private PreCOrgExtraDao preOrgExtraDao;
	
	@Autowired
	private ProCOrgExtraDao proOrgExtraDao;
	
	@Override
	public List<COrgExtra> inCurrnetNotInHis370(String area) throws Exception{
		List<COrgExtra> preCurOrg = preOrgExtraDao.getOrgExtra370(area);
		List<COrgExtra> preHisOrg = preOrgExtraDao.getHisOrgExtra370(area);
		
		if(CollectionUtils.isEmpty(preHisOrg)){
			throw new RuntimeException("预生产当前表未查到记录");
		}
		if(CollectionUtils.isEmpty(preHisOrg)){
			throw new RuntimeException("预生产历史表未查到记录");
		}
		
		Iterator<COrgExtra> it = preCurOrg.iterator();
		while(it.hasNext()){
			COrgExtra org = it.next();
			for(COrgExtra orgHis : preHisOrg){
				//案号
				String caseNo = org.getCaseNo().trim();
				Date caseDate = org.getCaseDate();
				//立案时间
				String caseNoHis = orgHis.getCaseNo().trim();
				Date caseDateHis = orgHis.getCaseDate();
				//案号和时间同时相等，存在于历史表中
				if(StringUtils.equals(caseNo, caseNoHis) 
						&&DateUtils.isSameDay(caseDate, caseDateHis)){
					it.remove();
				}
			}
		}
		return preCurOrg;
	}
	
	
	@Override
	public List<COrgExtra> getPreCurrnetInfo370(String area) {
		return preOrgExtraDao.getOrgExtra370(area);
	}
	
	@Override
	public List<COrgExtra> getProCurrnetInfo370(String area) {
		return proOrgExtraDao.getHisOrgExtra370(area);
	}
}
